package org.jeecg.modules.demo.base.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.demo.base.entity.ZnzfApprovalAppartAdmin;
import org.jeecg.modules.demo.base.entity.ZnzfApprovalOrRejectParam;
import org.jeecg.modules.demo.base.entity.ZnzfEmployeeScoreItem;
import org.jeecg.modules.demo.base.service.IZnzfApprovalAppartAdminService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.demo.base.service.IZnzfScoreStandardItemService;
import org.jeecg.modules.demo.definedtool.Paging;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 部门审核专员设定
 * @Author: jeecg-boot
 * @Date:   2022-03-27
 * @Version: V1.0
 */
@Api(tags="部门审核专员设定")
@RestController
@RequestMapping("/base/znzfApprovalAppartAdmin")
@Slf4j
public class ZnzfApprovalAppartAdminController extends JeecgController<ZnzfApprovalAppartAdmin, IZnzfApprovalAppartAdminService> {
	@Autowired
	private IZnzfApprovalAppartAdminService znzfApprovalAppartAdminService;

	@Autowired
	private ISysUserService sysUserService;

	@Autowired
	private IZnzfScoreStandardItemService znzfScoreStandardItemService;
	
	/**
	 * 分页列表查询
	 *
	 * @param znzfApprovalAppartAdmin
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "部门审核专员设定-分页列表查询")
	@ApiOperation(value="部门审核专员设定-分页列表查询", notes="部门审核专员设定-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<ZnzfApprovalAppartAdmin>> queryPageList(ZnzfApprovalAppartAdmin znzfApprovalAppartAdmin,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<ZnzfApprovalAppartAdmin> queryWrapper = QueryGenerator.initQueryWrapper(znzfApprovalAppartAdmin, req.getParameterMap());
		Page<ZnzfApprovalAppartAdmin> page = new Page<ZnzfApprovalAppartAdmin>(pageNo, pageSize);
		IPage<ZnzfApprovalAppartAdmin> pageList = znzfApprovalAppartAdminService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 * 新分页列表查询
	 *
	 * @param req
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@ApiOperation(value="部门审核专员设定-新分页列表查询", notes="部门审核专员设定-新分页列表查询")
	@GetMapping(value = "/voList")
	public Result<Page<ZnzfApprovalAppartAdmin>> queryVoPageList(HttpServletRequest req,
																 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
																 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
		List<ZnzfApprovalAppartAdmin> list = znzfApprovalAppartAdminService.queryAllApprovalDepartAdmin();
		Paging<ZnzfApprovalAppartAdmin> paging = new Paging<>();
		Result<Page<ZnzfApprovalAppartAdmin>> result = paging.getPaging(list, pageNo, pageSize);
		return result;
	}
	
	/**
	 *   添加
	 *
	 * @param znzfApprovalAppartAdmin
	 * @return
	 */
	@AutoLog(value = "部门审核专员设定-添加")
	@ApiOperation(value="部门审核专员设定-添加", notes="部门审核专员设定-添加")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody ZnzfApprovalAppartAdmin znzfApprovalAppartAdmin) {
		znzfApprovalAppartAdminService.save(znzfApprovalAppartAdmin);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param znzfApprovalAppartAdmin
	 * @return
	 */
	@AutoLog(value = "部门审核专员设定-编辑")
	@ApiOperation(value="部门审核专员设定-编辑", notes="部门审核专员设定-编辑")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody ZnzfApprovalAppartAdmin znzfApprovalAppartAdmin) {
		znzfApprovalAppartAdminService.updateById(znzfApprovalAppartAdmin);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "部门审核专员设定-通过id删除")
	@ApiOperation(value="部门审核专员设定-通过id删除", notes="部门审核专员设定-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		znzfApprovalAppartAdminService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "部门审核专员设定-批量删除")
	@ApiOperation(value="部门审核专员设定-批量删除", notes="部门审核专员设定-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.znzfApprovalAppartAdminService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "部门审核专员设定-通过id查询")
	@ApiOperation(value="部门审核专员设定-通过id查询", notes="部门审核专员设定-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<ZnzfApprovalAppartAdmin> queryById(@RequestParam(name="id",required=true) String id) {
		ZnzfApprovalAppartAdmin znzfApprovalAppartAdmin = znzfApprovalAppartAdminService.getById(id);
		if(znzfApprovalAppartAdmin==null) {
			return Result.error("未找到对应数据",null);
		}
		return Result.OK(znzfApprovalAppartAdmin);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param znzfApprovalAppartAdmin
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ZnzfApprovalAppartAdmin znzfApprovalAppartAdmin) {
        return super.exportXls(request, znzfApprovalAppartAdmin, ZnzfApprovalAppartAdmin.class, "部门审核专员设定");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ZnzfApprovalAppartAdmin.class);
    }


	 /**
	  *   分页查看所有资质审核信息
	  *
	  * @param
	  * @return
	  */
	 @AutoLog(value = "部门审核专员设定-分页查看所有审核信息")
	 @ApiOperation(value="部门审核专员设定-分页查看所有审核信息", notes="部门审核专员设定-分页查看所有审核信息")
	 @PostMapping(value = "/queryAllEmployeeScoreItems")
	 public Result<Page<ZnzfEmployeeScoreItem>> queryAllEmployeeScoreItems(HttpServletRequest request,
																		   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
																		   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {


		 String token = request.getHeader(CommonConstant.X_ACCESS_TOKEN);
		 String tmp = JwtUtil.getUsername(token);
		 SysUser sysUser = sysUserService.getUserByName(tmp);
		 ZnzfApprovalAppartAdmin admin = znzfApprovalAppartAdminService.getAdminByUserId(sysUser.getId());
		 // 无权限
		 if (admin == null) {
		 	return Result.error("暂无该项资质查询权限", null);
		 }
	 	 List<ZnzfEmployeeScoreItem> itemList = znzfApprovalAppartAdminService.queryAllEmployeeScoreItems();
		 if (itemList.isEmpty()) {
		 	return Result.error("未查到相应数据", null);
		 }
		 Paging<ZnzfEmployeeScoreItem> resPage = new Paging<>();
		 Result<Page<ZnzfEmployeeScoreItem>> paging = resPage.getPaging(itemList, pageNo, pageSize);
		 return paging;
	 }



	 /**
	  * 根据scoreItemId查着所有审核信息(分页)
	  * @param request
	  * @param scoreItemId
	  * @param pageNo
	  * @param pageSize
	  * @return
	  */
	 @AutoLog(value = "部门审核专员设定-根据scoreItemId查着所有审核信息")
	 @ApiOperation(value="部门审核专员设定-根据scoreItemId查着所有审核信息", notes="部门审核专员设定-根据scoreItemId查着所有审核信息")
	 @PostMapping(value = "/queryEmployeeScoreItemsByItemId")
	 public Result<Page<ZnzfEmployeeScoreItem>> queryEmployeeScoreItemsByItemId(HttpServletRequest request,
																		   @RequestParam(name = "scoreItemId", required = true) String scoreItemId,
																		   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
																		   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {

		 String token = request.getHeader(CommonConstant.X_ACCESS_TOKEN);
		 String tmp = JwtUtil.getUsername(token);
		 SysUser sysUser = sysUserService.getUserByName(tmp);
		 ZnzfApprovalAppartAdmin admin = znzfApprovalAppartAdminService.getAdminByUserId(sysUser.getId());
		 // 无权限
		 if (admin == null) {
			 return Result.error("暂无该项资质查询权限", null);
		 }
		 List<ZnzfEmployeeScoreItem> itemList = znzfApprovalAppartAdminService.queryEmployeeScoreItemsByItemId(scoreItemId);
		 if (itemList.isEmpty()) {
			 return Result.error("未查到相应数据", null);
		 }
		 Paging<ZnzfEmployeeScoreItem> resPage = new Paging<>();
		 Result<Page<ZnzfEmployeeScoreItem>> paging = resPage.getPaging(itemList, pageNo, pageSize);
		 return paging;
	 }



	 /**
	  * 通过或拒绝某条资质
	  * @param request
	  * @param znzfApprovalOrRejectParam
	  * @return
	  */
	 @AutoLog(value = "部门审核专员设定-通过或拒绝某条资质")
	 @ApiOperation(value="部门审核专员设定-通过或拒绝某条资质", notes="部门审核专员设定-通过或拒绝某条资质")
	 @PostMapping(value = "/setEmployeeScoreItemState")
	 public Result setEmployeeScoreItemState(HttpServletRequest request,
											 @RequestBody ZnzfApprovalOrRejectParam znzfApprovalOrRejectParam) {
		 String token = request.getHeader(CommonConstant.X_ACCESS_TOKEN);
		 String tmp = JwtUtil.getUsername(token);
		 SysUser sysUser = sysUserService.getUserByName(tmp);
		 ZnzfApprovalAppartAdmin admin = znzfApprovalAppartAdminService.getAdminByUserId(sysUser.getId());
		 // 无权限
		 if (admin == null) {
			 return Result.error("暂无该项资质查询权限", null);
		 }
		 // 设置职员资质，接受或拒绝
		 int res = znzfApprovalAppartAdminService.setState(znzfApprovalOrRejectParam, sysUser.getRealname());
		 if (res == 0) {
		 	return Result.error("审核失败", null);
		 } else if (res == 1) {
		 	return Result.ok("审核成功,已通过");
		 } else {
		 	return Result.ok("审核成功,已拒绝");
		 }
	 }
 }
